import { getprofile, getUserDetailById, login } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    Uptoken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    updateUserinfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async doLogin(context, loginForm) {
      const { data: res } = await login(loginForm)
      context.commit('Uptoken', res.data)
      return res
    },
    async postProfile(context) {
      const { data: res } = await getprofile()
      const { data: info } = await getUserDetailById(res.data.userId)
      // console.log(info)
      context.commit('updateUserinfo', { ...res.data, ...info.data })
      return { ...res.data, ...info.data }
    },
    logOut(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  },
  getters: {

  }
}
